package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Maps;
import com.ruoyi.system.service.IDrugModelService;
import com.ruoyi.system.service.IDrugRuleResolveService;
import com.ruoyi.system.service.IDrugRuleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.domain.Drug;
import com.ruoyi.system.service.IDrugService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 药品目录Controller
 *
 * @author wmb
 * @date 2020-07-27
 */
@RestController
@RequestMapping("/drug")
public class DrugController extends BaseController {
    @Autowired
    private IDrugService drugService;

    @Autowired
    private IDrugRuleService drugRuleService;

    @Autowired
    private IDrugRuleResolveService drugRuleResolveService;

    /**
     * 查询药品目录列表
     */
    //@PreAuthorize("@ss.hasPermi('system:drug:list')")
    @GetMapping("/list")
    public TableDataInfo list(Drug drug) {
        startPage();
        List<Drug> list = drugService.selectDrugList(drug);
        return getDataTable(list);
    }

    /**
     * 导出药品目录列表
     */
    @PreAuthorize("@ss.hasPermi('system:drug:export')")
    @Log(title = "药品目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Drug drug) throws IOException {
        List<Drug> list = drugService.selectDrugList(drug);
        ExcelUtil<Drug> util = new ExcelUtil<Drug>(Drug.class);
        util.exportExcel(response, list, "drug");
    }

    /**
     * 获取药品目录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:drug:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(drugService.selectDrugById(id));
    }

    /**
     * 新增药品目录
     */
    @PreAuthorize("@ss.hasPermi('system:drug:add')")
    @Log(title = "药品目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Drug drug) {
        return toAjax(drugService.insertDrug(drug));
    }

    /**
     * 修改药品目录
     */
    @PreAuthorize("@ss.hasPermi('system:drug:edit')")
    @Log(title = "药品目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Drug drug) {
        return toAjax(drugService.updateDrug(drug));
    }

    /**
     * 删除药品目录
     */
    @PreAuthorize("@ss.hasPermi('system:drug:remove')")
    @Log(title = "药品目录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(drugService.deleteDrugByIds(ids));
    }


    @GetMapping(value = "/drugStat")
    public AjaxResult drugStat() {
        Integer drugCount = drugService.count();
        Integer drugRuleCount = drugRuleService.count();
        Integer drugRuleResolveCount = drugRuleResolveService.count();

        Map<String, Integer> map = Maps.newHashMap();
        map.put("drugCount", drugCount);
        map.put("drugRuleCount", drugRuleCount);
        map.put("drugRuleResolveCount", drugRuleResolveCount);
        return AjaxResult.success(map);
    }


}
